package com.example.day1.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.day1.DTO.DishDto;
import com.example.day1.common.R;
import com.example.day1.entity.Category;
import com.example.day1.entity.Dish;
import com.example.day1.entity.DishFlavor;
import com.example.day1.service.CategoryService;
import com.example.day1.service.DishFlavorService;
import com.example.day1.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/dish")
public class DishController {
    @Autowired
    private DishService dishService;
    @Autowired
    private DishFlavorService dishFlavorService;

    @Autowired
    private CategoryService categoryService;
        @PostMapping
        //将json数据处理后放入两张表中
    public R<String> add(@RequestBody DishDto dishDto) {
            Dish dish = new Dish();
//            dish.setCategoryId(dishDto);
            dishService.savewith(dishDto);
            return R.success("新增菜品成功");
        }

        @GetMapping("/page")
        public R<Page> page(int page,int pageSize,String name){

            Page<Dish> objectPage = new Page<>(page, pageSize);

            Page<DishDto> dishDtoPage = new Page<>();

            LambdaQueryWrapper<Dish> objectLambdaQueryWrapper = new LambdaQueryWrapper<>();
            objectLambdaQueryWrapper.like(name!=null,Dish::getName,name);
            objectLambdaQueryWrapper.orderByDesc(Dish::getUpdateTime);
            dishService.page(objectPage,objectLambdaQueryWrapper);
            //由于返回值的类型不满足要求，所以就改变返回类型
            //复制将以由的dish属性复制给dto
            BeanUtils.copyProperties(objectPage,dishDtoPage,"recorders");
            List<Dish> records = objectPage.getRecords();
            List<DishDto > list= records.stream().map((item)->{

                DishDto dishDto = new DishDto();
                BeanUtils.copyProperties(item,dishDto);
                Long categoryId = item.getCategoryId();
                Category category=categoryService.getById(categoryId);
                String categoryname = category.getName();
                dishDto.setCategoryName(categoryname);
                return dishDto;
            }).collect(Collectors.toList());

            dishDtoPage.setRecords(list);
            return R.success( dishDtoPage);
        }
        @GetMapping("/{id}")
    public R<DishDto> get(@PathVariable long id){
            DishDto dishDtoR = dishService.get(id);

        return R.success(dishDtoR);
        }
        @PutMapping
    public R<String> get(@RequestBody DishDto dishDto){
            dishService.updatewith(dishDto);
            return R.success("修改菜品成功");
        }

    /**
     * 根据条件查询菜品数据
     * @param dish
     * @return
     */
//    @GetMapping("/list")
//    public R<List<Dish>> list(Dish dish){
//        LambdaQueryWrapper<Dish> objectLambdaQueryWrapper = new LambdaQueryWrapper<>();
//        objectLambdaQueryWrapper.eq(dish.getCategoryId()!=null,Dish::getCategoryId,dish.getCategoryId());
//        objectLambdaQueryWrapper.orderByAsc(Dish::getSort);
//        //指定筛选条件显示
//        objectLambdaQueryWrapper.eq(Dish::getStatus,1);
//        List<Dish> list = dishService.list(objectLambdaQueryWrapper);
//        return R.success(list);
//    }
    //需要修改，要设置返回值里有口味信息，名称信息，用于前端页面显示
    @GetMapping("/list")
    public R<List<DishDto>> list(Dish dish){
        LambdaQueryWrapper<Dish> objectLambdaQueryWrapper = new LambdaQueryWrapper<>();
        objectLambdaQueryWrapper.eq(dish.getCategoryId()!=null,Dish::getCategoryId,dish.getCategoryId());
        objectLambdaQueryWrapper.orderByAsc(Dish::getSort);
        //指定筛选条件显示
        objectLambdaQueryWrapper.eq(Dish::getStatus,1);
        List<Dish> list = dishService.list(objectLambdaQueryWrapper);

        List<DishDto> list1= list.stream().map((item)->{

            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(item,dishDto);
            Long categoryId = item.getCategoryId();
            Category category=categoryService.getById(categoryId);
            if(category!=null) {
                String categoryname = category.getName();
                dishDto.setCategoryName(categoryname);
            }
            Long id = item.getId();
            LambdaQueryWrapper<DishFlavor> objectLambdaQueryWrapper1 = new LambdaQueryWrapper<>();
            objectLambdaQueryWrapper1.eq(DishFlavor::getDishId,id);
            List<DishFlavor> list2 = dishFlavorService.list(objectLambdaQueryWrapper1);
            dishDto.setFlavors(list2);
            return dishDto;
        }).collect(Collectors.toList());

        return R.success(list1);
    }

}
